*********************************************************************************
*                                                                               *
*                  Election Frequency - UK Case Study 		     	            *
*                                                                               *
*          	               		Analysis  							  			*
*                                                                               *
*********************************************************************************

global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'  // F - Home

 cd `"${beg_path}Data\Analysis 2A"'
 
*** Uploading the data ***

use UK_dataset_final.dta, clear


*** Preparing the data *** 

*Region variables 
encode(Constituency), gen(constit)
fre constit 

encode(Region), gen(REGION)
lab var REGION "Region" 
gen Wales = REGION == 10 
lab var Wales "Wales" 
gen Scotland = REGION == 7
lab var Scotland "Scotland" 
gen England = inlist(REGION, 1, 2, 3, 5, 6, 8, 9, 11, 12)
lab var England "England" 
gen N_Ireland = inlist(REGION, 4)
lab var N_Ireland "Northern Ireland" 

*Getting rid of non-English constituencies (not studied by the paper)
drop if Wales == 1 | Scotland == 1 | N_Ireland ==1 

*Constituency c_type 
encode(Type), gen(c_type) 
lab var c_type "Constituency type"

*Registered voters (ln)
gen Registered_log = ln(Registered)
lab var Registered_log "Registered voters (ln)" 

*Creating variables that identify the treatment for the entire dataset 
replace local_el = 0 if inlist(Year, 2010, 2019)
replace local_authorities_el =0 if inlist(Year, 2010, 2019)
gen Local = local_el 
lab var local_el "England-Treatment" 

*Turnout in different years as time-invariant variables for each constituency
gen var_temp1 = Turnout if Year == 2015
egen Turnout_2015 = mode(var_temp1), by(constit)
lab var Turnout_2015 "Turnout in 2015" 

gen var_temp2 = Turnout if Year == 2010
egen Turnout_2010 = mode(var_temp2), by(constit)
lab var Turnout_2010 "Turnout in 2010" 

gen var_temp3 = Turnout if Year == 2019
egen Turnout_2019 = mode(var_temp3), by(constit)
lab var Turnout_2019 "Turnout in 2019" 

***********************************************************************
*** Visual inspection of the parallel trends assumption (Figure XY) ***
***********************************************************************

*Creating variables useful for inspecting the parallel trends assumptions
egen temp1 = max(local_el), by(constit) // creating a variable that identify treated constituencies in 2017 in England for all election years  

cap set scheme bw // Setting a graph scheme (not essential for the replication)
preserve
collapse Turnout, by(temp1 Year Wales Scotland England)
tw connected Turnout Year if temp1 ==1 & England == 1 || connected Turnout Year if temp1 ==0 & England == 1, name(graph1, replace) xlabel(2010 2015 2017 2019, grid) /// 
legend(lab(1 "England-Treatment: local elections in 2017") lab(2 "England-Control: no local elections in 2017") position(6) size(vsmall) row(1)) ytitle("Turnout (%)") xtitle("Election year", margin(medsmall)) ylabel(62(2)72) 
restore 
 
cd `"${beg_path}/Results"'

graph export "Figure_5.eps", replace  // width(3000) 


***********************************************************
*** Formal inspection of the parallel trends assumption *** 
***********************************************************
 

**************** 
*** Table A8 *** 
****************

eststo clear 
eststo M1: reg Turnout temp1 if inlist(Year, 2010) 
estadd local fixed "No" , replace 
eststo M2: reg Turnout temp1 if inlist(Year, 2015)
estadd local fixed "No" , replace 
eststo M3: reg Turnout temp1 if inlist(Year, 2017)
estadd local fixed "No" , replace 
eststo M4: reg Turnout temp1 if inlist(Year, 2019)
estadd local fixed "No" , replace 
eststo M5: areg Turnout i.temp1##ib2010.Year  if REGION!=4, absorb(ID) cluster(ID)
estadd local fixed "Yes" , replace 

esttab M1 M2 M3 M4 M5 using A_Table_8.tex, one incelldelimite("\:") stats(fixed N r2, labels("Constituency FE" "N" "R2") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2)) replace b(2) se(2) wide staraux ///
		star(* 0.05 ** 0.01 *** 0.001) eqlabels(none) order(temp1 1.temp1#2015.Year 1.temp1#2017.Year 1.temp1#2019.Year 2015.Year  2017.Year  2019.Year) /// 
		keep(temp1 1.temp1#2015.Year 1.temp1#2017.Year 1.temp1#2019.Year 2015.Year  2017.Year  2019.Year) ///
		varlabels(temp1 "England-Tratment" 1.temp1#2015.Year "England Treatment in 2015 (placebo)" 1.temp1#2017.Year "England Treatment in 2017" 1.temp1#2019.Year "England Treatment in 2019 (placebo)" ///
		2015.Year "2015" 2017.Year "2017" 2019.Year "2019" _cons "Constant") nolegend note("") alignment(d{10}) mtitles("2010" "2015" "2017" "2019" "All Years")

 
  
		
***************
*** Table 2 *** 
***************
*Last preparation 
gen placebo_in_2015 = temp1==1 & Year == 2015
gen placebo_in_2010 = temp1==1 & Year == 2010
gen Placebo_in_2019 = temp1==1 & Year == 2019 
		
recode Year (2010 = 1) (2015 = 2) (2017 = 3) (2019 = 4), gen(el_number)		
tsset constit el_number		

eststo clear 
eststo M1: reg d.Turnout d.local_el if Year==2017 //  
estadd local controls "No"
estadd local LDV "No"

eststo M2: reg d.Turnout d.local_el   d.Registered_log d.difference d.CON d.LAB d.LD d.UKIP Brexit_Leave_Han  if Year==2017 // local_el Wales Scotland  local_el Wales Scotland 
estadd local controls "Yes"
estadd local LDV "No"

eststo M3: reg d.Turnout d.coverage if Year==2017 
estadd local controls "No"
estadd local LDV "No"

eststo M4: reg d.Turnout d.coverage  d.Registered_log d.difference d.CON d.LAB d.LD d.UKIP Brexit_Leave_Han if Year==2017 
estadd local controls "Yes"
estadd local LDV "No"

eststo M5: reg Turnout local_el  c.Turnout_2015 c.Turnout_2015#c.Turnout_2015  if Year==2017 // Wales Scotland 
estadd local controls "No"
estadd local LDV "Yes"

eststo M6: reg Turnout local_el c.Turnout_2015 c.Turnout_2015#c.Turnout_2015  Registered_log i.c_type difference  CON LAB LD UKIP Brexit_Leave_Han if Year==2017 // Wales Scotland  
estadd local controls "Yes"
estadd local LDV "Yes"

eststo M7: reg Turnout coverage Turnout_2015 c.Turnout_2015#c.Turnout_2015 if Year==2017 // Coverage_Wales Coverage_Scotland 
estadd local controls "No"
estadd local LDV "Yes"

eststo M8: reg Turnout coverage Turnout_2015 c.Turnout_2015#c.Turnout_2015 Registered_log i.c_type difference CON LAB LD UKIP Brexit_Leave_Han if Year==2017 // Coverage_Wales Coverage_Scotland
estadd local controls "Yes"
estadd local LDV "Yes"

eststo M9: reg Turnout placebo_in_2015 Turnout_2010 c.Turnout_2010#c.Turnout_2010  if Year==2015  // placebo_Wales placebo_Scotland
estadd local controls "No"
estadd local LDV "Yes"

eststo M10: reg Turnout placebo_in_2015 Turnout_2010 c.Turnout_2010#c.Turnout_2010 Registered_log i.c_type difference CON LAB LD UKIP Brexit_Leave_Han if Year==2015 // placebo_Wales placebo_Scotland
estadd local controls "Yes"
estadd local LDV "Yes"

esttab M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 using Table_2.tex, one incelldelimite("\:") stats(LDV controls N r2, labels("LDV" "Conrols" "N" "R2") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 0 2))  replace b(2) se(2) wide staraux ///
		star( * 0.05 ** 0.01 *** 0.001) eqlabels(none) nobase order(D.local_el local_el D.coverage coverage placebo_in_2015 ) ///
		keep(D.local_el D.coverage local_el  coverage placebo_in_2015  _cons) ///
		varlabels(D.local_el "England-Treatment (FD)" local_el "England-Treatment" /// 
		D.coverage  "Coverage: England Treatment (FD)" coverage  "Coverage: England-Treatment"  placebo_in_2015 "England-Treatment" /// 
		 _cons "Constant") nolegend note("") /// 
		refcat(D.local_el "\textit{Treatment}" D.coverage "\textit{Treatment: coverage}" placebo_in_2015 "\textit{Placebo}", nolabel) /// nonumbers
		mgroups("Test: Turnout in 2017" "Placebo: Turnout in 2015", pattern(1 0 0 0 0 0 0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("FD Model" "FD Model" "FD Model"  "FD Model"  "LDV" "LDV" "LDV" "LDV" "LDV" "LDV" )  alignment(d{10})


**************** 
*** Table A9 *** 
****************		
		
esttab M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 using A_Table_9.tex, one incelldelimite("\:") stats(LDV N r2, labels("LDV"  "N" "R2") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2))  replace b(2) se(2) wide staraux ///
		star( * 0.05 ** 0.01 *** 0.001) eqlabels(none) nobase order(D.local_el local_el D.coverage coverage placebo_in_2015 D.Registered_log D.difference  D.CON D.LAB D.LD D.UKIP Brexit_Leave_Han  Registered_log difference  CON LAB LD UKIP Brexit_Leave_Han Turnout_2015    c.Turnout_2015#c.Turnout_2015 Turnout_2010 c.Turnout_2010#c.Turnout_2010 2.c_type  _cons ) ///
		keep(D.local_el D.coverage local_el  coverage placebo_in_2015 D.Registered_log D.difference  D.CON D.LAB D.LD D.UKIP Brexit_Leave_Han  Registered_log 2.c_type difference  CON LAB LD UKIP Brexit_Leave_Han Turnout_2015 c.Turnout_2015#c.Turnout_2015 Turnout_2010 c.Turnout_2010#c.Turnout_2010  _cons) ///
		varlabels(D.local_el "England-Treatment (FD)" local_el "England-Treatment" /// 
		D.coverage  "Coverage: England-Treatment (FD)" coverage  "Coverage: England-Treatment"  placebo_in_2015 "England-Treatment" ///
		D.Registered_log "FD_Registered voters (ln)" D.difference "FD_Margin of victory" D.CON "FD_Share: Conservatives" /// 
		D.LAB "FD_Share: Labour" D.LD "FD_Share: LibDem" D.UKIP "FD_Share: UKIP" Brexit_Leave_Han "Share: Brexit" /// 
		Registered_log "Registered voters (ln)"  2.c_type "County constituency" difference "Margin of victory" ///
		CON "Share: Conservatives" LAB "Share: Labour" LD "Share: LibDem" UKIP "Share: UKIP" Brexit_Leave_Han /// 
		"Share: Brexit" Turnout_2015 "Turnout in 2015" c.Turnout_2015#c.Turnout_2015 "Turnout in 2015^2" ///
		Turnout_2010 "Turnout in 2010" c.Turnout_2010#c.Turnout_2010 "Turnout in 2010^2"  _cons "Constant", ///
		elist(placebo_in_2015 \hline )) nolegend note("") /// 
		refcat(D.local_el "\textit{Treatment}" D.coverage "\textit{Treatment: coverage}" ///
		placebo_in_2015 "\textit{Placebo}" D.Registered_log "\textit{Controls}", nolabel) /// nonumbers
		mgroups("Test: Turnout in 2017" "Placebo: Turnout in 2015", pattern(1 0 0 0 0 0 0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("FD Model" "FD Model" "FD Model"  "FD Model"  "LDV" "LDV" "LDV" "LDV" "LDV" "LDV" )  alignment(d{10})
		
	 
		
******************************
*** Descriptive Statistics *** 
******************************		
		
*Descriptive statistics (Table)
recode c_type (1=0) (2=1), gen(county)
lab var county "County constituency"
lab var BREXITLeave "Brexit" 
lab var difference "Margin of victory"
lab var coverage "Coverage: England-Treatment" 
lab var CON "Share: Conservatives" 
lab var LAB "Share: Labour" 
lab var LD "Share: LibDem" 
lab var UKIP "Share: UKIP" 
lab var Brexit_Leave_Han "Share: Brexit" 

* All England
quietly estpost sum local_el coverage  Turnout_2015 Registered_log county difference CON LAB LD UKIP BREXITLeave if Year==2017
eststo M1

*England: Treatment 
quietly estpost sum local_el coverage  Turnout_2015 Registered_log county difference CON LAB LD UKIP BREXITLeave if Year==2017 & local_el==1
eststo M2

*England: Control
quietly estpost sum local_el coverage  Turnout_2015 Registered_log county difference CON LAB LD UKIP BREXITLeave if Year==2017 & local_el==0
eststo M3

**************** 
*** Table A7 *** 
****************

esttab M1 M2 M3 using A_Table_7.tex, replace cells("count(fmt(%9.0f)) mean(fmt(%9.2f)) sd(fmt(%9.2f)) min(fmt(%9.2f)) max(fmt(%9.2f))" ) label varwidth(30) noobs ///
collabels("N" "Mean" "SD" "Min" "Max") nodep nonumb  modelwidth(6) mtitles("All Constituencies" "England-Treatment" "England-Control")
